Best Host Hub

How to Install Git on Ubuntu 22.04 and 24.04

How to Install Git on Ubuntu 22.04 and 24.04


Git is available in Ubuntu’s default package repositories on both 22.04 LTS and 24.04, so installation is fast. The more involved part is configuring SSH key authentication for GitHub or GitLab, which is what this guide covers in full. You’ll also find a section on managing multiple Git identities on a single server, which comes…

Prerequisites

A Linux VPS or cloud server running Ubuntu 22.04 LTS or Ubuntu 24.04

SSH access with sudo privileges

A GitHub or GitLab account (for the SSH key configuration steps)

Both Ubuntu 22.04 LTS and 24.04 ship with OpenSSH client pre-installed. No additional tools are required before beginning.

Not set up on a VPS yet? InMotion Hosting’s Cloud VPS includes Ubuntu 22.04 LTS as a supported OS. The Managed VPS supports Ubuntu across all plan tiers.

Step 1: Update Package Lists

Before installing any package, sync your apt package index to ensure you’re pulling the latest available version.

sudo apt update

Step 2: Install Git

sudo apt install git y

The -y flag confirms the installation automatically. On Ubuntu 22.04, this installs Git 2.34.x. On 24.04, you’ll get Git 2.43.x. To verify the installed version:

git version

Expected output will look similar to: git version 2.43.0

Step 3: Configure Your Git Identity

Git requires a name and email address attached to every commit. Set these globally for the server user you’ll be working with.

Verify the configuration:

git config list

Step 4: Set Up SSH Key Authentication

Cloning and pushing to remote repositories over HTTPS requires a password or token on every operation. SSH keys authenticate once and never prompt again. This is the standard approach for server-to-repository authentication.

4a. Generate the SSH key pair

sshkeygen t ed25519 C “[email protected]”

The -t ed25519 flag specifies the Ed25519 algorithm, which is the modern recommended key type. When prompted for a file location, press Enter to accept the default (~/.ssh/id_ed25519). Optionally set a passphrase.

If your remote service requires RSA (older GitLab instances, for example), use:

sshkeygen t rsa b 4096 C “[email protected]”

4b. Display your public key

cat ~/.ssh/id_ed25519.pub

Copy the entire output, including the ssh-ed25519 prefix and the comment at the end.

4c. Add the key to GitHub or GitLab

On GitHub: Settings > SSH and GPG keys > New SSH key. Paste your public key and give it a descriptive title (for example, ‘InMotion VPS production’).

On GitLab: User Settings > SSH Keys. Paste the key, give it a title, and set an expiration date if your security policy requires one.

4d. Test the connection

Expected output: Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.

Expected output: Welcome to GitLab, @username!

Step 5: Clone a Repository

With SSH authentication configured, cloning uses the SSH URL rather than HTTPS.

git clone [email protected]:yourusername/your-repo.git

The repository will clone into a directory named after the repo. Navigate into it and confirm the remote configuration:

cd yourrepo && git remote v

Managing Multiple Git Identities on One Server

Development VPS environments frequently need to authenticate as different users for different repositories. A developer account for personal projects, a CI deploy key for a client’s repository, a separate identity for work. The SSH config file handles this cleanly.

Create a per-host SSH config

nano ~/.ssh/config

Add a block for each host identity:

Host githubpersonal  HostName github.com  User git  IdentityFile ~/.ssh/id_ed25519Host githubclient  HostName github.com  User git  IdentityFile ~/.ssh/id_ed25519_client

Generate the second key pair with a different filename:

sshkeygen t ed25519 f ~/.ssh/id_ed25519_client C “[email protected]”

Add id_ed25519_client.pub to the client’s GitHub or GitLab account under their deploy keys.

Use the alias when cloning

git clone git@githubclient:clientorg/theirrepo.git

SSH resolves github-client to github.com and uses the specified key file. This approach works without any conflict between identities, even on the same physical server.

Setting a Default Branch Name

Git’s default branch name changed from ‘master’ to ‘main’ in newer configurations, but repositories created on older setups may still use ‘master’. To align your server’s Git behavior with your team’s convention, set the default branch name globally:

git config global init.defaultBranch main

Optional: Install a Newer Version via PPA

Ubuntu’s default repositories may not include the absolute latest Git release. If you need a newer version for specific features, the Git Maintainers PPA provides current builds for Ubuntu LTS releases:

sudo addaptrepository ppa:gitcore/ppasudo apt updatesudo apt install git

This is optional for most use cases. The version in Ubuntu’s default repos is stable and sufficient for the vast majority of workflows.

Related guide: How to Setup a VPS Server covers the full VPS setup workflow from provisioning through deployment.

Run Git on production-ready infrastructure. InMotion’s Cloud VPS includes Ubuntu 22.04 LTS with root SSH access, high-availability architecture, and no-nonsense pricing. See plans at inmotionhosting.com/cloud-vps.



Source link

Leave a Comment

Your email address will not be published. Required fields are marked *