Installing npm on Ubuntu
To install npm (the Node.js package manager) on Ubuntu, follow one of the clean, up-to-date methods below.
Option 1: Install from NodeSource (Recommended)
This gives you the latest stable Node.js and npm.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# Update your system sudo apt update # Install curl if missing sudo apt install -y curl # Add the NodeSource setup script for the current LTS (e.g. Node 20) curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - # Install Node.js (includes npm) sudo apt install -y nodejs |
Verify installation:
|
1 2 3 4 |
node -v npm -v |
Option 2: Install via Ubuntu’s Default Repositories (Older Version)
This is simpler but often outdated:
|
1 2 3 4 |
sudo apt update sudo apt install -y nodejs npm |
Check versions:
|
1 2 3 4 |
node -v npm -v |
If npm shows a very old version, you can update it later with:
|
1 2 3 |
sudo npm install -g npm@latest |
Installing OpenAI SDK (Codex Successor)
The @openai/codex package is no longer supported. Instead, install the official OpenAI SDK to access GPT-4 and GPT-5 models.
Option 1: Install Locally (Recommended for Projects)
|
1 2 3 4 5 |
mkdir myproject && cd myproject npm init -y npm install openai |
Then you can use it in JavaScript:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import OpenAI from "openai"; const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY }); const response = await client.chat.completions.create({ model: "gpt-4o-mini", messages: [{ role: "user", content: "Write a short JavaScript function to reverse a string." }] }); console.log(response.choices[0].message.content); |
Before running, set your API key:
|
1 2 3 |
export OPENAI_API_KEY="your_api_key_here" |
Option 2: Global Installation (Optional)
If you prefer a global install without sudo:
|
1 2 3 4 |
mkdir "${HOME}/.npm-global" npm config set prefix "${HOME}/.npm-global" |
Add this line to your ~/.bashrc or ~/.zshrc:
|
1 2 3 |
export PATH=$PATH:$HOME/.npm-global/bin |
Reload your shell:
|
1 2 3 |
source ~/.bashrc |
Now you can safely install globally:
|
1 2 3 |
npm install -g openai |
Installing Google Gemini SDK
To use Google’s Gemini models, install the official Generative AI SDK:
|
1 2 3 |
npm install @google/generative-ai |
Example usage:
|
1 2 3 4 5 6 7 8 9 |
import { GoogleGenerativeAI } from "@google/generative-ai"; const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY); const model = genAI.getGenerativeModel({ model: "gemini-1.5-pro" }); const result = await model.generateContent("Write a function in Python that prints Fibonacci numbers."); console.log(result.response.text()); |
Before running, export your Gemini API key:
|
1 2 3 |
export GEMINI_API_KEY="your_api_key_here" |
Summary
| Goal | Install Command | SDK | API Key Env Variable |
|---|---|---|---|
| Use GPT-4/5 (Codex successor) | npm install openai |
OpenAI SDK | OPENAI_API_KEY |
| Use Google Gemini | npm install @google/generative-ai |
Google Generative AI SDK | GEMINI_API_KEY |