In this tutorial we will be learning how to write a simple Hello World application in every programming language.
We will also be going over how to install the necessary dependencies and any setup required for each language.
For each language I assume you will create a new empty directory to store your code.
I will also be using macOS and Homebrew to install certain dependencies. You can find the Homebrew website for more information.
HTML
For HTML the requirements are pretty easy. Any web browser will be able to run our Hello World example.
Just create an index.html
file with the following contents:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World</title>
</head>
<body>
<p>Hello World!</p>
</body>
</html>
Then simply open your browser and open the index.html
file.
JavaScript
For JavaScript we are going to use Node.js. To install Node.js we are going to use nvm.
Run the following command to install nvm, then restart your Terminal:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
Then run the following command to install Node.js (replacing 16.13.1 with the latest version found on the Node.js website):
nvm install 16.13.1
Then create a file called index.js
with the following contents:
console.log("Hello World!");
Then run the following command to run the application:
node index.js
Swift
Run which swift
to see if you already have swift installed. If not, run the following to install swift:
brew install swift
Then create a file called index.swift
with the following contents:
print("Hello World!")
Then run the following command to run the application:
swift index.swift
C
Run which clang
to see if you already have clang installed. If not, run the following to install clang:
brew install llvm --with-clang --with-asan
Then create a file called index.c
with the following contents:
#include <stdio.h>
int main() {
printf("Hello World!\n");
}
Then you can compile the file by running:
clang -g index.c -o index
Then run the application:
./index
It will print Hello World!
to the console.
C++
Run which clang++
to see if you already have clang installed. If not, run the following to install clang:
brew install llvm --with-clang --with-asan
Then create a file called index.cpp
with the following contents:
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!" << endl;
return 0;
}
Then you can compile the file by running:
clang++ -g index.cpp -o index
Then run the application:
./index
Python
Check to see if you have Python installed by running which python3
. If not, run the following to install Python:
brew install [email protected]
Then create a file called index.py
with the following contents:
print("Hello World!")
Java
Download the Java JDK. For my Apple Silicon computer I had to download this from here.
You can run java --version
to verify you have everything working correctly.
Then create a file called index.java
with the following contents:
class Index {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Then run the following command to run the application:
java index.java
Kotlin
Run which kotlin
to see if you already have Kotlin installed. If not run the following to install Kotlin:
brew install kotlin
Then create a file called index.kt
with the following contents:
fun main(args: Array) {
println("Hello World!")
}
Then compile the application with the following command:
kotlinc index.kt -include-runtime -d index.jar
Then run the following command to run the application:
java -jar index.jar
Bash
Create a file called index.sh
with the following contents:
echo "Hello World!"
Next we need to give the script permission to run. We can do this by using the chmod
command:
chmod +x index.sh
Finally, we can run the following command to run the application:
./index.sh
Go
Run which go
to see if you already have go installed. If not run the following to install go:
brew install go
Then create a file called index.go
with the following contents:
package main
import "fmt"
func main() {
fmt.Println("Hello World!")
}
You can compile the file by running:
go build index.go
Then run the application:
./index
You can also do both of these steps at once by running:
go run index.go
Rust
Run which rustc
to see if you already have Rust installed. If not, run the following to install Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Then create a file called index.rs
with the following contents:
fn main() {
println!("Hello World!");
}
You can compile the file by running:
rustc index.rs
Then run the application:
./index
Ruby
Create a file called index.rb
with the following contents:
puts "Hello World!"
Then run the following command to run the application:
ruby index.rb
Dart
Run which dart
to see if you already have Dart installed. If not, run the following to install Dart:
brew tap dart-lang/dart
brew install dart
Then create a file called index.dart
with the following contents:
main() {
print("Hello World!");
}
Then run the following command to run the application:
dart index.dart
Perl
Create a file called index.pl
with the following contents:
print "Hello World!\n";
Then run the following command to run the application:
perl index.pl
Assembly
This is likely only to work with Apple Silicon devices. Assembly is basically as low level of a programming language as it gets. Be careful!!
Create a file called index.s
with the following contents:
//
// Assembler program to print "Hello World!"
// to stdout.
//
// X0-X2 - parameters to linux function services
// X16 - linux function number
//
.global _start // Provide program starting address to linker
.align 2
// Setup the parameters to print hello world
// and then call Linux to do it.
_start: mov X0, #1 // 1 = StdOut
adr X1, helloworld // string to print
mov X2, #13 // length of our string
mov X16, #4 // MacOS write system call
svc 0 // Call linux to output the string
// Setup the parameters to exit the program
// and then call Linux to do it.
mov X0, #0 // Use 0 return code
mov X16, #1 // Service command code 1 terminates this program
svc 0 // Call MacOS to terminate the program
helloworld: .ascii "Hello World!\n"
Then create a file called Makefile
with the following contents:
index: index.o
ld -macosx_version_min 11.0.0 -o index index.o -lSystem -syslibroot `xcrun -sdk macosx --show-sdk-path` -e _start -arch arm64
index.o: index.s
as -o index.o index.s
Then run the following command to compile/make the application:
make -B
Then run the following command to run the application:
./index
You can find all of the code on my GitHub repository I created for this project.